<?php
namespace App\Http\Controllers\Api\v1\Recode;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\UserinfoController;
use Illuminate\Http\Request;
use App\Models\Api\Recode;

use DB;
// 星盘档案管理控制器
class NoteController extends BaseController
{
    public function __construct()
    {

            //parent::__construct();// 接口调用验证

    }
    /**
     * 添加笔记注释
     */
    public function add(Request $request)
    {
        $content = htmlspecialchars($request->input('content'));
        $rid = intval($request->input('rid'));
        $time = time();
        $event_time = $request->input('event_time',0);
        $from = htmlspecialchars($request->input('from'));
         $uid = $request->uid;
        $data = [
            'uid'=>$uid,
            'content'=>$content,
            'rid'=>$rid,
            'from'=>$from,
            'create_time'=>$time,
            'last_time'=>$time,
            'event_time'=>$event_time
        ];
        if(!$uid)
        {
            return response()->json(['code'=>10401,'User is not exist.']);
        }
        if(!$rid)
        {
            return response()->json(['code'=>10402,'Recode is not exist.']);
        }
        if(trim($content) == '')
        {
            return response()->json(['code'=>10403,'Content can not be empty.']);
        }
        $r_data = Recode::where(['id'=>$rid])->first();
        if(empty($r_data))
        {
            return response()->json(['code'=>10404,'msg'=>'Recode is not exist.']);
        }
        $data['id'] = DB::table('app_recode_note')->insertGetId($data);
        if(!$data['id'])
        {
            return response()->json(['code'=>10405,'msg'=>'Failed.']);
        }
        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }
    /**
     * 修改笔记注释
     */
    public function edit(Request $request)
    {
        $id = intval($request->input('id'));
        $content = htmlspecialchars($request->input('content'));
        $rid = intval($request->input('rid'));
        $time = time();
        $data = [
            'content'=>$content,
            'last_time'=>$time,
        ];
        if(trim($content) == '')
        {
            return response()->json(['code'=>10407,'Content can not be empty.']);
        }
         $uid = $request->uid;

        $r_data =Recode::where(['id'=>$rid])->find(1);
        if(empty($r_data))
        {
            return response()->json(['code'=>10408,'msg'=>'Recode is not exist.']);
        }
        $n_data = DB::table('app_recode_note')->where(['id'=>$id,'uid'=>$uid])->find();
        if(empty($n_data))
        {
            return response()->json(['code'=>10409,'msg'=>'Recode Note is not exist.']);
        }
        DB::table('app_recode_note')->where(['id'=>$id,'uid'=>$uid])->update($data);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }
    /**
     * 删除注释
     */
    public function del(Request $request)
    {
        $id = intval($request->input('id'));
        $n_data = DB::table('app_recode_note')->where(['id'=>$id])->first();
        if(empty($n_data))
        {
            return response()->json(['code'=>10411,'msg'=>'Recode Note is not exist.']);
        }
         $uid = $request->uid;
        if($uid != $n_data->uid)
        {
            return response()->json(['code'=>10412,'msg'=>'You are not the note owner.']);
        }
        if(DB::table('app_recode_note')->where(['id'=>$id,'uid'=>$uid])->delete())
        {
            return response()->json(['code'=>200,'msg'=>'Delete Success.']);
        }
        return response()->json(['code'=>10413,'msg'=>'Delete Failed.']);
    }
    /**
     * 查询该档案下所有笔记注释
     */
    public function lists(Request $request)
    {
        $rid = intval($request->input('rid'));
        $uid = $request->uid;
        $data = DB::table('app_recode_note')->where(['rid'=>$rid,'uid'=>$uid])->orderBy('create_time', 'DESC')->get();
        if(!$rid)
        {
            return response()->json(['code'=>10414,'msg'=>'Recode is not exist.']);
        }
        if(!$uid)
        {
            return response()->json(['code'=>10415,'msg'=>'User is not exist..']);
        }
        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }

    /**
     * 统计注释的数量
     * @param $rid int 该档案下注释的数量
     */
    public function counts(Request $request)
    {
         $rid = intval($request->input('rid'));
         $uid = $request->uid;
        $count = DB::table('app_recode_note')->where(['rid'=>$rid,'uid'=>$uid])->count('id');
        return response()->json(['code'=>200,'data'=>$count,'msg'=>'Success.']);
    }
}
